<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 7.1.1: Counting problems with Poisson distribution</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch07_statistical_estim/html/counting_problem_poisson.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 7.1.1: Counting problems with Poisson distribution</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Jo&Atilde;&laquo;lle Skaf - 04/24/08</span>
<span class="comment">%</span>
<span class="comment">% The random variable y is nonnegative and integer valued with a Poisson</span>
<span class="comment">% distribution with mean mu &gt; 0. In a simple statistical model, the mean mu</span>
<span class="comment">% is modeled as an affine function of a vector u: mu = a'*u + b.</span>
<span class="comment">% We are given a number of observations which consist of pairs (u_i,y_i),</span>
<span class="comment">% i = 1,..., m, where y_i is the observed value of y for which the value of</span>
<span class="comment">% the explanatory variable is u_i. We find a maximum likelihood estimate of</span>
<span class="comment">% the model parameters a and b from these data by solving the problem</span>
<span class="comment">%           maximize    sum_{i=1}^m (y_i*log(a'*u_i + b) - (a'*u_i + b))</span>
<span class="comment">% where the variables are a and b.</span>

<span class="comment">% Input data</span>
rand(<span class="string">'state'</span>,0);
n = 10;
m = 100;
atrue = rand(n,1);
btrue = rand;

u = rand(n,m);
mu = atrue'*u + btrue;

<span class="comment">% Generate random variables y from a Poisson distribution</span>
<span class="comment">% (The distribution is actually truncated at 10*max(mu) for simplicity)</span>
L  = exp(-mu);
ns = ceil(max(10*mu));
y  = sum(cumprod(rand(ns,m))&gt;=L(ones(ns,1),:));

<span class="comment">% Maximum likelihood estimate of model parameters</span>
cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span>
    maximize <span class="string">sum(y.*log(a'*u+b) - (a'*u+b))</span>
cvx_end
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Successive approximation method to be employed.
   For improved efficiency, SDPT3 is solving the dual problem.
   SDPT3 will be called several times to refine the solution.
   Original size: 276 variables, 103 equality constraints
   92 exponentials add 736 variables, 460 equality constraints
-----------------------------------------------------------------
 Cones  |             Errors              |
Mov/Act | Centering  Exp cone   Poly cone | Status
--------+---------------------------------+---------
 92/ 92 | 6.764e-01  3.304e-02  0.000e+00 | Solved
 90/ 92 | 5.602e-02  2.250e-04  0.000e+00 | Solved
 55/ 88 | 1.386e-03  1.261e-07  0.000e+00 | Solved
  0/ 34 | 1.389e-04  1.143e-09  0.000e+00 | Solved
-----------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +102.57
 
</pre>
</div>
</body>
</html>